/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import database.DBClass;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Koperasi;
import model.Search;
import validation.ValidateForm;

/**
 *
 * @author lenovo
 */
public class SearchServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    //declare values to get form values from jsp page
    String inputsearch;
    String range1;
    String range2;
    String action;
    DBClass dbobject = new DBClass();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the values from jsp page
            inputsearch = request.getParameter("inputsearch");
            range1 = request.getParameter("range1");
            range2 = request.getParameter("range2");
            action = request.getParameter("action");
            if (action.equals("SearchAll")) {
                request.setAttribute("Search", inputsearch);
                request.setAttribute("searchtype","all");
                RequestDispatcher rd = request.getRequestDispatcher("/search.jsp");
                rd.forward(request, response);
                //now display errors in that jsp page
            }
            if (action.equals("searchjenis")) {
                //get userdetails of particular name
                request.setAttribute("Search", inputsearch);
                request.setAttribute("searchtype","produk");
                request.setAttribute("produktype","jenis");
                RequestDispatcher rd = request.getRequestDispatcher("/search.jsp");
                rd.forward(request, response);
            }
            if (action.equals("searchharga")) {
                //delete userdetails of particular name
                request.setAttribute("Range1", range1);
                request.setAttribute("Range2", range2);
                request.setAttribute("searchtype","produk");
                request.setAttribute("produktype","harga");
                RequestDispatcher rd = request.getRequestDispatcher("/search.jsp");
                rd.forward(request, response);
            }
             if (action.equals("searchkota")) {
                //delete userdetails of particular name
                request.setAttribute("Search",inputsearch);
                request.setAttribute("searchtype","kota");
                RequestDispatcher rd = request.getRequestDispatcher("/search.jsp");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            out.println(e);
            System.out.println(e);
        } finally {
            out.close();
        }
    }
    //this method is used to setvalues to produk

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}
